<template>
    <div>
        <h1 style="color: brown;">科室-添加</h1>
        <table class="table">
            <thead>
                <tr>
                    <td><span style="color: red;">*</span> 图片</td>
                    <td>
                        <input  type="file" @change="FileChange">
                        <img :src="'https://localhost:7187'+obj.img" style="height: 120px; width: 130;">
                    </td>
                </tr>
                <tr>
                    <td><span style="color: red;">*</span> 成立时间</td>
                    <td>
                        <input v-model="obj.createTime" type="date" >
                    </td>
                </tr>
                <tr>
                    <td><span style="color: red;">*</span> 科室名称</td>
                    <td>
                        <input v-model="obj.officeName" type="text">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value="添加" @click="add" class="btn">
                    </td>
                </tr>
            </thead>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
const router=useRouter();
const route=useRoute();

const obj=ref({
    "id": 0,
    "img": "",
    "createTime": "2025-02-18T06:48:33.346Z",
    "officeName": ""
})

const add=()=>{
    if(obj.value.img==""){
        alert('图片不得为空')
        return;
    }

    if(obj.value.createTime==""){
        alert('成立时间不得为空')
        return;
    }
    if(obj.value.officeName==""){
        alert('科室名称不得为空')
        return;
    }
    if(obj.value.officeName=="耳科"){
        alert('科室名称不能重复')
        return;
    }


    axios.post("https://localhost:7187/api/Office/AddOffice",obj.value).then((res)=>{
        if(res.data>0){
            alert('添加成功')
            router.push("/ShowOffice");
        }
        else{
            alert('添加失败')
        }
    })
}

const FileChange=(e:any)=>{
    var f = e.target.files[0];
    var fd = new FormData();
    fd.append("file", f);

    if(f.size>1024*1024*2){
        alert('图片最大只能是2MB');
        return;
    }

    if(f.type=='image/jpeg'||f.type=='image/png'||f.type=='image/gif'){
        axios.post("https://localhost:7187/api/Office/UpFile",fd).then(res=>{
        obj.value.img=res.data;
     })
    }
    else{
        alert('只能上传jpeg||png格式')
    }
}
</script>

<style scoped>

</style>